package org.gdstash.ui.stash;

import org.gdstash.file.DDSLoader;
import org.gdstash.item.GDItem;
import org.gdstash.item.GDItemContainer;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.GDUITransfer;
import org.gdstash.util.GDImagePool;

import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
import java.util.List;

public class GDEquippedContainerPane extends JLabel implements GDUIContainer {
    private static Rectangle[] itemRectangles;
    protected GDItemContainer container;
    protected int containerType;
    protected GDUIInventory uiInventory;
    protected GDUITransfer uiTransfer;
    protected Rectangle mouseRect;
    protected int xOffset;
    protected int yOffset;
    protected GDStashFrame frame;

    public GDEquippedContainerPane(final int xOffset, final int yOffset, final GDStashFrame frame, final GDUIInventory uiInventory, final GDUITransfer uiTransfer) {
        this.frame = frame;
        this.containerType = 6;
        this.xOffset = xOffset;
        this.yOffset = yOffset;
        this.uiInventory = uiInventory;
        this.uiTransfer = uiTransfer;
        this.mouseRect = null;
        this.addMouseListener(new GDContainerPageMouseListener());
        this.addMouseMotionListener(new GDContainerPageMouseListener());
    }

    private int getItemPosition(final GDItem item) {
        if (item == null) {
            return -1;
        }
        int pos = item.getY();
        if (item.getX() >= 1) {
            pos += 12;
        }
        if (item.getX() >= 2) {
            pos += 2;
        }
        return pos;
    }

    private int getPositionFromCoord(final int x, final int y) {
        for (int i = 0; i < GDEquippedContainerPane.itemRectangles.length; ++i) {
            if (GDEquippedContainerPane.itemRectangles[i].contains(x, y)) {
                return i;
            }
        }
        return -1;
    }

    private GDItem getItemFromCoord(final int x, final int y) {
        GDItem posItem = null;
        if (this.container != null) {
            for (int i = 0; i < GDEquippedContainerPane.itemRectangles.length; ++i) {
                if (GDEquippedContainerPane.itemRectangles[i].contains(x, y)) {
                    for (final GDItem item : this.container.getItemList()) {
                        if (item != null) {
                            final int pos = this.getItemPosition(item);
                            if (pos != i) {
                                continue;
                            }
                            posItem = item;
                        }
                    }
                }
            }
        }
        return posItem;
    }

    @Override
    public void setContainer(final GDItemContainer container) {
        if (container == null) {
            return;
        }
        if (container.getContainerType() != 6) {
            return;
        }
        this.container = container;
        this.containerType = container.getContainerType();
        this.layoutPage();
    }

    @Override
    public BufferedImage getBackgroundImage() {
        return GDImagePool.imgCharEquipped;
    }

    @Override
    public BufferedImage drawGraphics() {
        GDItem selItem = null;
        if (this.uiTransfer != null) {
            selItem = this.uiTransfer.getSelectedItem();
        }
        final BufferedImage imgBG = this.getBackgroundImage();
        final BufferedImage image = new BufferedImage(imgBG.getWidth(), imgBG.getHeight(), imgBG.getType());
        final Graphics2D g = image.createGraphics();
        if (this.container == null) {
            g.setColor(GDContainerPane.COLOR_DARK_RED);
            g.setComposite(AlphaComposite.getInstance(10, 0.8f));
            g.fillRect(0, 0, imgBG.getWidth(), imgBG.getHeight());
        }
        g.drawImage(imgBG, 0, 0, null);
        Rectangle rect = null;
        if (this.container != null) {
            for (final GDItem item : this.container.getItemList()) {
                final BufferedImage img = item.getFullImage();
                if (img != null) {
                    final int pos = this.getItemPosition(item);
                    final int x = GDEquippedContainerPane.itemRectangles[pos].x;
                    final int y = GDEquippedContainerPane.itemRectangles[pos].y;
                    final int w = GDEquippedContainerPane.itemRectangles[pos].width;
                    final int h = GDEquippedContainerPane.itemRectangles[pos].height;
                    final int xpos = x + (GDEquippedContainerPane.itemRectangles[pos].width - img.getWidth()) / 2;
                    final int ypos = y + (GDEquippedContainerPane.itemRectangles[pos].height - img.getHeight()) / 2;
                    if (img != null) {
                        final Color color = item.getOverlayColor();
                        if (color != null) {
                            g.setColor(color);
                            g.setComposite(AlphaComposite.getInstance(10, 0.3f));
                            g.fillRect(x + 1, y + 1, w - 2, h - 2);
                            g.setComposite(AlphaComposite.getInstance(10, 1.0f));
                            g.drawRect(x + 1, y + 1, w - 2, h - 2);
                        }
                        g.drawImage(img, xpos, ypos, null);
                    }
                    if (item != selItem) {
                        continue;
                    }
                    rect = GDEquippedContainerPane.itemRectangles[pos];
                }
            }
        }
        if (rect != null) {
            g.setColor(GDContainerPane.COLOR_GREEN);
            g.drawRect((int) rect.getX(), (int) rect.getY(), (int) rect.getWidth(), (int) rect.getHeight());
            g.drawRect((int) rect.getX() + 1, (int) rect.getY() - 1, (int) rect.getWidth() - 2, (int) rect.getHeight() + 2);
        }
        if (this.mouseRect != null) {
            if (this.isBlocked()) {
                g.setColor(GDContainerPane.COLOR_RED);
            } else {
                g.setColor(GDContainerPane.COLOR_WHITE);
            }
            g.drawRect((int) this.mouseRect.getX(), (int) this.mouseRect.getY(), (int) this.mouseRect.getWidth(), (int) this.mouseRect.getHeight());
            g.drawRect((int) this.mouseRect.getX() + 1, (int) this.mouseRect.getY() - 1, (int) this.mouseRect.getWidth() - 2, (int) this.mouseRect.getHeight() + 2);
        }
        return image;
    }

    @Override
    public int getXOffset() {
        return this.xOffset;
    }

    @Override
    public int getYOffset() {
        return this.yOffset;
    }

    @Override
    public int getHeight() {
        return this.getPreferredHeight();
    }

    @Override
    public int getWidth() {
        return this.getPreferredWidth();
    }

    @Override
    public boolean addItem(final GDItem item, final int action, final int xCoord, final int yCoord) {
        return false;
    }

    @Override
    public boolean deleteItem(final GDItem item, final int action, final boolean update) {
        return false;
    }

    @Override
    public boolean hasChanged() {
        return this.container != null && this.container.hasChanged();
    }

    @Override
    public List<GDItem> getItemList(final int action) {
        final List<GDItem> list = new LinkedList<GDItem>();
        if (this.container == null) {
            return list;
        }
        return this.container.getItemList();
    }

    @Override
    public void refresh() {
        if (this.container != null) {
            this.container.refresh();
        }
    }

    static {
        (GDEquippedContainerPane.itemRectangles = new Rectangle[16])[0] = new Rectangle(226, 3, 73, 72);
        GDEquippedContainerPane.itemRectangles[1] = new Rectangle(312, 3, 38, 37);
        GDEquippedContainerPane.itemRectangles[2] = new Rectangle(89, 151, 73, 104);
        GDEquippedContainerPane.itemRectangles[3] = new Rectangle(89, 281, 73, 104);
        GDEquippedContainerPane.itemRectangles[4] = new Rectangle(363, 313, 73, 72);
        GDEquippedContainerPane.itemRectangles[5] = new Rectangle(363, 232, 73, 72);
        GDEquippedContainerPane.itemRectangles[6] = new Rectangle(175, 54, 38, 37);
        GDEquippedContainerPane.itemRectangles[7] = new Rectangle(312, 54, 38, 37);
        GDEquippedContainerPane.itemRectangles[8] = new Rectangle(226, 375, 73, 40);
        GDEquippedContainerPane.itemRectangles[9] = new Rectangle(363, 151, 73, 72);
        GDEquippedContainerPane.itemRectangles[10] = new Rectangle(312, 375, 41, 40);
        GDEquippedContainerPane.itemRectangles[11] = new Rectangle(172, 375, 41, 40);
        GDEquippedContainerPane.itemRectangles[12] = new Rectangle(3, 3, 73, 134);
        GDEquippedContainerPane.itemRectangles[13] = new Rectangle(363, 3, 73, 134);
        GDEquippedContainerPane.itemRectangles[14] = new Rectangle(89, 3, 73, 134);
        GDEquippedContainerPane.itemRectangles[15] = new Rectangle(449, 3, 73, 134);
    }

    @Override
    public Dimension getPreferredSize() {
        final BufferedImage image = this.getBackgroundImage();
        final int w = image.getWidth() * GDStashFrame.iniConfig.sectUI.graphicScale / 100;
        final int h = image.getHeight() * GDStashFrame.iniConfig.sectUI.graphicScale / 100;
        return new Dimension(w, h);
    }

    @Override
    public Dimension getMaximumSize() {
        return this.getPreferredSize();
    }

    public int getPreferredWidth() {
        return (int) this.getPreferredSize().getWidth();
    }

    public int getPreferredHeight() {
        return (int) this.getPreferredSize().getHeight();
    }

    public void layoutPage() {
        if (this.uiTransfer == null) {
            this.mouseRect = null;
        } else if (this.uiTransfer.getSelectedItem() == null) {
            this.mouseRect = null;
        }
        BufferedImage image = this.drawGraphics();
        if (GDStashFrame.iniConfig.sectUI.graphicScale != 100) {
            final int w = image.getWidth() * GDStashFrame.iniConfig.sectUI.graphicScale / 100;
            final int h = image.getHeight() * GDStashFrame.iniConfig.sectUI.graphicScale / 100;
            image = DDSLoader.getScaledImage(image, w, h);
        }
        this.setIcon(new ImageIcon(image));
    }

    private boolean isBlocked() {
        return true;
    }

    @Override
    public void updateConfig() {
    }

    private class GDContainerPageMouseListener extends MouseInputAdapter {
        @Override
        public void mousePressed(final MouseEvent e) {
            this.selectItem(e);
        }

        @Override
        public void mouseMoved(final MouseEvent e) {
            GDItem item = null;
            if (GDEquippedContainerPane.this.uiTransfer != null) {
                item = GDEquippedContainerPane.this.uiTransfer.getSelectedItem();
            }
            if (item == null) {
                return;
            }
            final BufferedImage img = item.getImage();
            if (img == null) {
                return;
            }
            int x = e.getX();
            int y = e.getY();
            int xOff = GDEquippedContainerPane.this.getXOffset();
            int yOff = GDEquippedContainerPane.this.getYOffset();
            int w = GDEquippedContainerPane.this.getWidth();
            int h = GDEquippedContainerPane.this.getHeight();
            if (GDStashFrame.iniConfig.sectUI.graphicScale != 100) {
                xOff = xOff * GDStashFrame.iniConfig.sectUI.graphicScale / 100;
                yOff = yOff * GDStashFrame.iniConfig.sectUI.graphicScale / 100;
            }
            if (x < xOff || x > xOff + w || y < yOff || y > yOff + h) {
                return;
            }
            x -= xOff;
            y -= yOff;
            if (GDStashFrame.iniConfig.sectUI.graphicScale != 100) {
                x = x * 100 / GDStashFrame.iniConfig.sectUI.graphicScale;
                y = y * 100 / GDStashFrame.iniConfig.sectUI.graphicScale;
                w = w * 100 / GDStashFrame.iniConfig.sectUI.graphicScale;
                h = h * 100 / GDStashFrame.iniConfig.sectUI.graphicScale;
            }
            for (int i = 0; i < GDEquippedContainerPane.itemRectangles.length; ++i) {
                if (GDEquippedContainerPane.itemRectangles[i].contains(x, y)) {
                    GDEquippedContainerPane.this.mouseRect = GDEquippedContainerPane.itemRectangles[i];
                }
            }
            GDEquippedContainerPane.this.layoutPage();
        }

        private void selectItem(final MouseEvent e) {
            if (GDEquippedContainerPane.this.container == null) {
                return;
            }
            final int button = e.getButton();
            if (button == 3 && GDEquippedContainerPane.this.uiInventory != null) {
                GDEquippedContainerPane.this.uiInventory.setSelectedItem(null);
            }
            if (button != 1) {
                return;
            }
            int x = e.getX();
            int y = e.getY();
            int xOff = GDEquippedContainerPane.this.getXOffset();
            int yOff = GDEquippedContainerPane.this.getYOffset();
            final int w = GDEquippedContainerPane.this.getWidth();
            final int h = GDEquippedContainerPane.this.getHeight();
            if (GDStashFrame.iniConfig.sectUI.graphicScale != 100) {
                xOff = xOff * GDStashFrame.iniConfig.sectUI.graphicScale / 100;
                yOff = yOff * GDStashFrame.iniConfig.sectUI.graphicScale / 100;
            }
            if (x < xOff || x > xOff + w || y < yOff || y > yOff + h) {
                GDEquippedContainerPane.this.layoutPage();
                return;
            }
            x -= xOff;
            y -= yOff;
            if (GDStashFrame.iniConfig.sectUI.graphicScale != 100) {
                x = x * 100 / GDStashFrame.iniConfig.sectUI.graphicScale;
                y = y * 100 / GDStashFrame.iniConfig.sectUI.graphicScale;
            }
            GDItem selItem = null;
            if (GDEquippedContainerPane.this.uiTransfer != null) {
                selItem = GDEquippedContainerPane.this.uiTransfer.getSelectedItem();
            }
            GDItem item = GDEquippedContainerPane.this.getItemFromCoord(x, y);
            if (item != null) {
                GDEquippedContainerPane.this.uiInventory.setSelectedItem(item);
            }
            item = null;
            if (GDEquippedContainerPane.this.uiTransfer != null) {
                item = GDEquippedContainerPane.this.uiTransfer.getSelectedItem();
            }
            if (item != null && !GDEquippedContainerPane.this.isBlocked()) {
                GDEquippedContainerPane.this.mouseRect = null;
                if (GDEquippedContainerPane.this.uiTransfer.getItemLocation() == 1) {
                    GDEquippedContainerPane.this.uiTransfer.transferSelectedItem(1, e.getX(), e.getY());
                }
                if (GDEquippedContainerPane.this.uiTransfer.getItemLocation() == 2) {
                    GDEquippedContainerPane.this.uiTransfer.transferSelectedItem(4, e.getX(), e.getY());
                }
            }
        }
    }
}
